<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=514732
-->
<head>
  <title>Test for Bug 514732</title>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug?id=514732">Mozilla Bug 514732</a>

<div id="imgs" style="whitespace: nowrap; overflow: visible; height: 10px;"></div>

<iframe id="testframe" src="./file_bug514732_1.html"></iframe>

<br/><br/>
<div id="log">log:<br/></div>
<br/><br/>

<script class="testbody" style="width: 300; height: 150;" type="text/javascript;version=1.8">
// Import test API
var is = window.opener.is;
var ok = window.opener.ok;
var todo = window.opener.todo;
var finish = window.opener.SimpleTest.finish;

function log(msg) {
  document.getElementById("log").innerHTML += msg;
}

function logln(msg) {
  if (arguments.length == 0) msg = "";
  document.getElementById("log").innerHTML += msg + "<br/>";
}

function logev(ev) {
  logln([ev.x, ev.y, ev.width, ev.height].join(' '));
}

/* Absolutely position a 500x500 div at x, y on doc */
function phoom(doc, x, y) {
  var d = doc.createElement('div');
  d.id = "phoomer";
  d.style.position = "absolute";
  d.style.left = x + 'px';
  d.style.top = y + 'px';
  d.style.backgroundColor = "#00FFF0";
  d.style.width = 500 + 'px';
  d.style.height = 500 + 'px';
  doc.getElementById('imgs').appendChild(d);
}

function unphoom(doc) {
  var phoomer = doc.getElementById('phoomer');
  if (phoomer)
    doc.getElementById('imgs').removeChild(phoomer);
}

function getDocBodyDimensions(doc) {
  return [doc.documentElement.scrollWidth, doc.documentElement.scrollHeight];
}

function makeResult() {
  return {
    success: false,
    event: null
  };
}

function makeListener(result, eventGen) {
  return function(ev) {
    result.success = true;
    result.event = ev;
    setTimeout(function() { eventGen.next(); }, 0);
  };
}

function waitInterrupt(result, gen) {
  result.event = null;
  result.success = false;
  setTimeout(function() { if (!result.success) gen.next(); }, 3500);
}

function testPhoom(isCapturing, x, y, expectEvent) {

  var eventGen = (function() {
    var innerdoc = document.getElementById('testframe').contentDocument;

    for (var doc of [document, innerdoc]) {
      var inner = (doc == innerdoc);
      var w, h, result, listener;

      /* --- EXPANSION --- */

      result = makeResult();
      listener = makeListener(result, eventGen);

      doc.addEventListener("MozScrolledAreaChanged", listener, isCapturing);

      if (!expectEvent) waitInterrupt(result, eventGen);
      phoom(doc, x, y);
      yield;

      doc.removeEventListener("MozScrolledAreaChanged", listener, isCapturing);

      /* If we're expecting an event, better check that we got one.  If we are not expecting one,
         one can still arrive, but we don't complain if it did not.  In either case, any event
         that arrives will have its data checked below. */
      if (expectEvent) {
        ok(result.success, "Received expected " + (inner ? "inner" : "") + " expansion event");
      }

      if (result.success) {
        is(result.event.x, 0, "Expansion event x is 0");
        is(result.event.y, 0, "Expansion event y is 0");
        if (inner) {
          /* In iframe-land, the values of |myiframe.contentDocument.documentElement.scrollWidth| and
             |.scrollHeight| appear to be not as expected (they stay very small).  This is fine, since
             we know exactly where we're positioning our fixed-dimensions div, and we know our iframe's
             dimensions, so we can do our own math.  NB: The inner iframe is styled at 300x500. */
          is(Math.round(result.event.width),
             Math.max(x + 500, 300),
             "Inner expansion event width matches body width");
          is(Math.round(result.event.height),
             Math.max(y + 500, 150),
             "Inner expansion event height matches body height");
        } else {
          [w, h] = getDocBodyDimensions(doc);
          is(Math.round(result.event.width),  w, "Expansion event width matches body width");
          is(Math.round(result.event.height), h, "Expansion event height matches body height");
        }
      }

      /* --- CONTRACTION --- */

      result = makeResult();
      listener = makeListener(result, eventGen);

      doc.addEventListener("MozScrolledAreaChanged", listener, isCapturing);

      if (!expectEvent) waitInterrupt(result, eventGen);
      unphoom(doc);
      yield;

      doc.removeEventListener("MozScrolledAreaChanged", listener, isCapturing);

      if (expectEvent) {
        ok(result.success, "Received expected " + (inner ? "inner" : "") + " contraction event");
      }

      if (result.success) {
        is(result.event.x, 0, "Contraction event x is 0");
        is(result.event.y, 0, "Contraction event y is 0");
        if (inner) {
          is(Math.round(result.event.width),  300, "Inner contraction event width matches body width");
          is(Math.round(result.event.height), 150, "Inner contraction event height matches body height");
        } else {
          [w, h] = getDocBodyDimensions(doc);
          is(Math.round(result.event.width),  w, "Contraction event width matches body width");
          is(Math.round(result.event.height), h, "Contraction event height matches body height");
        }
      }
    }

    setTimeout(nextTest, 0);

    yield;
  })();

  setTimeout(function() { eventGen.next(); }, 0);
}

function testPreliminaries() {
  /* Do any setup here */
  nextTest();
}

function testEventCapturingHorz() {
  testPhoom(true, 10000, 0, true); // calls nextTest() when finished
}

function testEventCapturingVert() {
  testPhoom(true, 0, 10000, true); // calls nextTest() when finished
}

function testEventCapturingBoth() {
  testPhoom(true, 10000, 10000, true); // calls nextTest() when finished
}

function testEventCapturingNegv() {
  testPhoom(true, -10000, -10000, false); // calls nextTest() when finished
}

function testEventBubblingHorz() {
  testPhoom(false, 10000, 0, true); // calls nextTest() when finished
}

function testEventBubblingVert() {
  testPhoom(false, 0, 10000, true); // calls nextTest() when finished
}

function testEventBubblingBoth() {
  testPhoom(false, 10000, 10000, true); // calls nextTest() when finished
}

function testEventBubblingNegv() {
  testPhoom(false, -10000, -10000, false); // calls nextTest() when finished
}

function testCustomCreationHelper(creatingDoc, listeningDoc, x, y, w, h, expectEvent) {
  var result = makeResult();

  var listener = function(evt) {
    result.success = true;
    result.event = evt;
  };

  var ev = creatingDoc.createEvent("ScrollAreaEvent");
  ev.initScrollAreaEvent("MozScrolledAreaChanged",
                         true, true, window, 0,
                         x, y, w, h);

  listeningDoc.addEventListener("MozScrolledAreaChanged", listener, true);

  result.success = false;
  creatingDoc.dispatchEvent(ev);

  listeningDoc.removeEventListener("MozScrolledAreaChanged", listener, true);

  is(result.success, expectEvent, "Received custom event iff expected");
  if (result.success) {
    is(result.event.x,      x, "Custom event data matches for x");
    is(result.event.y,      y, "Custom event data matches for y");
    is(result.event.width,  w, "Custom event data matches for width");
    is(result.event.height, h, "Custom event data matches for height");
  }
}

function testCustomCreation() {
  var innerdoc = document.getElementById('testframe').contentDocument;
  testCustomCreationHelper(document, document, 2, 7, 1, 8, true);  // e = 2.718 ...
  testCustomCreationHelper(innerdoc, innerdoc, 2, 8, 1, 8, true);  //  ... 2818 ...
  testCustomCreationHelper(innerdoc, document, 2, 8, 4, 5, false); //  ... 2845 ...
  nextTest();
}

var TESTS = [testPreliminaries,
             testEventCapturingHorz,
             testEventCapturingVert,
             testEventCapturingBoth,
             testEventCapturingNegv,
             testEventBubblingHorz,
             testEventBubblingVert,
             testEventBubblingBoth,
             testEventBubblingNegv,
             testCustomCreation,
              __end];

var PC = 0;

function nextTest() {
  TESTS[PC++]();
}

function __end() {
  log("done!");
  finish();
  window.close();
}

/*
 * We need both the parent document and all iframe documents to load
 * before starting anything
 */
var loaded = 0;

window
.addEventListener('load', function(ev) {
  logln(++loaded);
  if (loaded >= 2) {
    logln("go...");
    nextTest();
  }
}, false);

document.getElementById("testframe").contentWindow
.addEventListener('load', function(ev) {
  logln(++loaded);
  if (loaded >= 2) {
    logln("go...");
    nextTest();
  }
}, false);

// window.opener says: SimpleTest.waitForExplicitFinish();

</script>

</body>
</html>
